/*
 *  Render.cpp
 *  TestApp
 *
 *  Created by liu dingxi on 09-7-30.
 *  Copyright 2009 tencent. All rights reserved.
 *
 */
#include <OpenGLES/ES1/gl.h>

#include "StdAfx.h"
#include "GameFrame.h"
#include "QQGame.h"

CQQGame theGame;

void GameInit(LPCSTR szFolder)
{	
	int nWidth = 320;
	int nHeight = 480;
	
	glClearColor(0, 0, 0, 1);
	
	glViewport(0, 0, nWidth, nHeight);
	glScissor(0, 0, nWidth, nHeight);
	glEnable(GL_SCISSOR_TEST);
	
	// setup OpenGL projection matrix
	glMatrixMode(GL_PROJECTION);
	glOrthof(0, nWidth, nHeight, 0, -100, 100);
	glMatrixMode(GL_MODELVIEW);
	
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	glEnable(GL_TEXTURE_2D);
	glDisable(GL_DEPTH_TEST);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnable(GL_BLEND);
	
	HGC hGC;
	hGC.nWidth = nWidth;
	hGC.nHeight = nHeight;
	hGC.pGC = NULL;
	
	theGame.Initialize(szFolder, hGC);
	
	theGame.OnLoad();
}

void GameRender()
{
	glClearColor(255, 255, 255, 1);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	
	theGame.Render();
	
	glFlush();
}

long GameUpdate(unsigned int nMillisecond)
{
	return theGame.Update(nMillisecond);
}

long GameMessage(unsigned int nMsg, unsigned long lParam1, unsigned long lParam2)
{
	long ret = theGame.OnMessage(nMsg, lParam1, lParam2);
	if(nMsg == MSG_QUIT)
	{
		theGame.OnQuit();
	}
	
	return ret;
}

FOCUS_EDIT* GameFocusEdit()
{
	GMsg* pMsg = theGame.GetMsg();
	if(pMsg)
	{
		return (FOCUS_EDIT*)pMsg->dwParam2;
	}
	return NULL;
}

void GameFree()
{
	theGame.Uninitialize();
}